﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Util;
using LeSoft.Application.Entity;
using LeSoft.Application.Code;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class WorkModuleController : MvcControllerBase
    {
        //
        // GET: /PropertyManage/WorkModule/
        private WorkModuleBLL bll = new WorkModuleBLL();
        #region 视图
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Form()
        {
            return View();
        }

        public ActionResult Model()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取模板树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "获取树", "GetTemplateTreeJson")]
        public ActionResult GetTemplateTreeJson(string queryJson)
        {
            var data = bll.GetList(queryJson);
            List<TreeEntity> treeList = new List<TreeEntity>();
            treeList.Add(new TreeEntity { complete = true, hasChildren = data != null && data.Count() > 0 ? true : false, id = "ALL", value = "ALL", text = "所有日志", title = "所有日志", parentId = "0" });
            foreach (var item in data)
            {
                TreeEntity tree = new TreeEntity();
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.title = item.Name;
                tree.parentId = "ALL";
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = false;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 分页获取列表
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">筛选条件</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "分页获取列表", "GetTemplatePageList")]
        public ActionResult GetTemplatePageList(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetForm, "获取实体", "GetFormJson")]
        public ActionResult GetTemplateForm(string keyValue)
        {
            var data = bll.GetForm(keyValue);
            var json = data.ToJson();
            if (data != null && !string.IsNullOrEmpty(data.FrmId))
            {
                var model = bll.GetFormModuleForm(data.FrmId);
                if (model != null)
                    json = new { data.Code, data.Description, data.DesignateData, data.Flags, data.Icon, data.Name, data.PermissionInfo, data.Sort, data.Version, WorkModuleInfo = model.FRMCONTENT }.ToJson();
            }
            return Content(json);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="workmodule">对象</param>
        /// <param name="WorkModuleInfo">自定义内容</param>
        /// <returns></returns>
        [HttpPost]

        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存模板", "SaveTemplateForm")]
        public ActionResult SaveTemplateForm(string keyValue, string workmodule, string WorkModuleInfo)
        {
            try
            {
                WorkModuleEntity model = workmodule.ToObject<WorkModuleEntity>();
                bll.SaveForm(keyValue, model, WorkModuleInfo);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error("操作失败。" + e.Message);
                throw;
            }
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Delete, "删除模板", "RemoveTemplateForm")]
        public ActionResult RemoveTemplateForm(string keyValue)
        {
            try
            {
                bll.RemoveForm(keyValue);
                return Success("操作成功。");
            }
            catch (Exception e)
            {
                return Error("操作失败。" + e.Message);
            }
        }
        #endregion
    }
}
